package com.example.project.service.impl;

import com.example.project.action.ActionUtil;
import com.example.project.base.BaseServiceImpl;
import com.example.project.base.ConstKit;
import com.example.project.bean.PVO;
import com.example.project.common.MatchesKey;
import com.example.project.common.ParamKey;
import com.example.project.entity.TAdmin_Role;
import com.example.project.entity.UserVO;
import com.example.project.mapper.UserMap;
import com.example.project.service.UserService;
import com.example.project.util.*;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl extends BaseServiceImpl<UserVO> implements UserService {
    @Autowired
    UserMap userMap;

    @Override
    public void userSort(Integer pkid, Integer sort) {
        if (sort.equals(1)) {
            userMap.up(pkid);
        } else {
            userMap.down(pkid);
        }
    }

    @Override
    public List<UserVO> getUserList(Map paramMap) {
        return userMap.getUserList(paramMap);
    }

    @Override
    public UserVO addUser(UserVO vo) {
        //检查电话号码是否重复
        Integer count = userMap.count(MapUtil.hashMap(new PVO(ParamKey.PHONE,vo.getPhone())));
        FunctionUtil.check(IntegerUtil.isNotEmpty(count),"该联系电话已注册，请重新输入！");
        vo.setAudit(1);//个人正常情况是不需要审核的，默认为1
        vo.setMiddle(0);//设置用户身份为个人用户
        if (StringUtil.isNotEmpty(vo.getInviteCode())) {
            //查询一下邀请码的有效性
            UserVO inviteVO = userMap.getUser(MapUtil.hashMap(new PVO(ParamKey.INVITE_CODE,vo.getInviteCode())));
            if (inviteVO != null) {
                FunctionUtil.check(!inviteVO.getAudit().equals(1),"该邀请码对应用户审核未通过，当前邀请码无效！");
                Integer integral = 5;//后期需要从配置里面获取
                if (inviteVO.getMiddle().equals(1)) {
                    vo.setMiddle(1);//将个人用户的身份更换为中间人
                    vo.setAudit(-1);//将当前用户的审核状态改为-1，需要审核
                    integral = 10;
                }
                //修改邀请码对应用户相应积分
                userMap.editUser(MapUtil.hashMap(new PVO(ParamKey.PK_ID,inviteVO.getPkid()),new PVO(ParamKey.INTEGRAL, inviteVO.getIntegral()+integral),
                        new PVO(ParamKey.UPDATE_BY,ActionUtil.getParameter(ParamKey.UPDATE_BY))));

                //添加邀请记录
                userMap.addMiddleman(MapUtil.hashMap(new PVO(ParamKey.INVITE_CODE,inviteVO.getInviteCode()),
                        new PVO(ParamKey.INVITER,inviteVO.getPhone()),
                        new PVO(ParamKey.PHONE,vo.getPhone()),
                        new PVO(ParamKey.MIDDLE,inviteVO.getMiddle()),
                        new PVO(ParamKey.CREATE_BY,ActionUtil.getParameter(ParamKey.CREATE_BY))));
            }
        }
        vo.setInviteCode(StringUtil.randomCodeNumber(8));
        vo.setAccount(vo.getPhone());
        vo.setPassword(MD5Util.generate(StringUtil.isNotEmpty(vo.getPassword())?vo.getPassword():ConstKit.DEFAULT_PWD));
        vo.setCreateBy(ActionUtil.getParameter(ParamKey.CREATE_BY));
        userMap.addUser(vo);
        return vo;
    }

    @Override
    public void editUser(Map paramMap) {
        FunctionUtil.check(StringUtil.isEmpty(paramMap.get(ParamKey.PK_ID)),"用户pkid为空！");
        FunctionUtil.matches(paramMap,ParamKey.PHONE, MatchesKey.PHONE_MATCHES,"联系电话不正确！如：18200001111");
        //检查电话号码是否重复
        Integer count = userMap.count(MapUtil.hashMap(new PVO(ParamKey.PHONE,paramMap.get(ParamKey.PHONE))));
        FunctionUtil.check(IntegerUtil.isNotEmpty(count),"该联系电话已注册，请重新输入！");
        if (!StringUtil.isEmpty(paramMap.get(ParamKey.INVITE_CODE))) {
            //查询一下邀请码的有效性
            UserVO inviteVO = userMap.getUser(MapUtil.hashMap(new PVO(ParamKey.INVITE_CODE,paramMap.get(ParamKey.INVITE_CODE))));
            if (inviteVO != null) {
                FunctionUtil.check(!inviteVO.getAudit().equals(1),"该邀请码对应用户审核未通过，当前邀请码无效！");
                Integer integral = 5;//后期需要从配置里面获取
                if (inviteVO.getMiddle().equals(1)) {
                    paramMap.put(ParamKey.MIDDLE,1);//将个人用户的身份更换为中间人
                    paramMap.put(ParamKey.AUDIT,-1);//将当前用户的审核状态改为-1，需要审核
                    integral = 10;
                }
                //修改邀请码对应用户相应积分
                userMap.editUser(MapUtil.hashMap(new PVO(ParamKey.PK_ID,inviteVO.getPkid()),new PVO(ParamKey.INTEGRAL, inviteVO.getIntegral()+integral),
                        new PVO(ParamKey.UPDATE_BY,ActionUtil.getParameter(ParamKey.UPDATE_BY))));
                //添加邀请记录
                userMap.addMiddleman(MapUtil.hashMap(new PVO(ParamKey.INVITE_CODE,inviteVO.getInviteCode()),
                        new PVO(ParamKey.INVITER,inviteVO.getPhone()),
                        new PVO(ParamKey.PHONE,paramMap.get(ParamKey.PHONE)),
                        new PVO(ParamKey.MIDDLE,inviteVO.getMiddle()),
                        new PVO(ParamKey.CREATE_BY,ActionUtil.getParameter(ParamKey.CREATE_BY))));
            }
        }
        userMap.editUser(paramMap);
    }
}
